//package com.woniuxy.microsysserver.serurity.filter;
//
//import com.woniuxy.microsysserver.serurity.security.TokenManager;
//import com.woniuxy.microsysserver.serurity.util.ResponseEntity;
//import com.woniuxy.microsysserver.serurity.util.ResponseUtil;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
//import org.springframework.util.StringUtils;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//
///**
// * <p>
// * 访问过滤器
// * </p>
// *
// * @author qy
// * @since 2019-11-08
// */
//public class TokenAuthenticationFilter extends BasicAuthenticationFilter {
//    private TokenManager tokenManager;
//    private RedisTemplate redisTemplate;
//
//
//    public TokenAuthenticationFilter (AuthenticationManager authManager, TokenManager tokenManager, RedisTemplate redisTemplate) {
//        super(authManager);
//        this.tokenManager = tokenManager;
//        this.redisTemplate = redisTemplate;
//    }
//
//    @Override
//    protected void doFilterInternal (HttpServletRequest req, HttpServletResponse res, FilterChain chain)
//            throws IOException, ServletException {
//        logger.info("=================" + req.getRequestURI());
//        if (req.getRequestURI().indexOf("userFont")!=-1 ){
//            System.out.println("用户放行");
//            chain.doFilter(req, res);
//            return;
//        }
//
//        if (("admin").equals(req.getSession().getAttribute("account"))) {
//            System.out.println("管理员放行");
//            chain.doFilter(req, res);
//            return;
//        }
//
//        UsernamePasswordAuthenticationToken authentication = null;
//        try {
//            authentication = getAuthentication(req);
//        } catch (Exception e) {
//            ResponseUtil.out(res, ResponseEntity.error());
//        }
//
//        if (authentication != null) {
//            SecurityContextHolder.getContext().setAuthentication(authentication);
//        } else {
//            ResponseUtil.out(res, ResponseEntity.error());
//        }
//        chain.doFilter(req, res);
//    }
//
//    private UsernamePasswordAuthenticationToken getAuthentication (HttpServletRequest request) {
//        // token置于header里
//        String token = request.getHeader("token");
//        if (token != null && ! "".equals(token.trim())) {
//            String userName = tokenManager.getUserFromToken(token);
//            //将当前用户名存入session
//            request.getSession().setAttribute("account", userName);
//            List<String> permissionValueList = (List<String>) redisTemplate.opsForValue().get(userName);
//            Collection<GrantedAuthority> authorities = new ArrayList<>();
//            for (String permissionValue : permissionValueList) {
//                if (StringUtils.isEmpty(permissionValue)) {
//                    continue;
//                }
//                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permissionValue);
//                authorities.add(authority);
//            }
//
//            if (! StringUtils.isEmpty(userName)) {
//                return new UsernamePasswordAuthenticationToken(userName, token, authorities);
//            }
//            return null;
//        }
//        return null;
//    }
//}
